/* $Revision: $ $Date: $
 * 
 * Copyright (C) 2009 Robert Stephan
 * 
 * This file is part of *** CaCaO - Card Catalogs Online ***
 *
 * CaCaO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CaCao is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cacao.client.ui;

import cacao.client.CaCaO;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;


public class LoginDialog extends DialogBox implements ClickHandler{
	private CaCaO application;
	private TextBox txtName;
	private PasswordTextBox txtPassword;
	private Button btnLogin;

	public LoginDialog(CaCaO app){
		application = app;
		// Set the dialog box's caption.
		setText(application.getMessages().loginDialogTitle());
		// DialogBox is a SimplePanel, so you have to set it's widget
		// property to whatever you want its contents to be.
		VerticalPanel panel = new VerticalPanel();
		HorizontalPanel hp = new HorizontalPanel();
		hp.setSpacing(10);
		VerticalPanel vp1 = new VerticalPanel();
		txtName = new TextBox();
		txtName.setWidth("150px");
		txtName.setTabIndex(1);
		vp1.add(txtName);
		Label lbl = new Label(application.getMessages().loginDialogUser());
		lbl.setWidth("150px");
		vp1.add(lbl);
		hp.add(vp1);

		VerticalPanel vp2 = new VerticalPanel();
		txtPassword = new PasswordTextBox();
		txtPassword.setWidth("150px");
		txtPassword.setTabIndex(2);
		vp2.add(txtPassword);
		Label lbl2 = new Label(application.getMessages().loginDialogPasswort());
		lbl2.setWidth("150px");
		vp2.add(lbl2);
		hp.add(vp2);
		panel.add(hp);
		panel.add(new Label(" "));

		DockPanel dpButtons = new DockPanel();
		btnLogin = new Button(application.getMessages().loginDialogLogin());

		btnLogin.setTabIndex(3);
		btnLogin.addClickHandler(this);
		dpButtons.add(btnLogin, DockPanel.CENTER);

		panel.add(dpButtons);
		setWidget(panel);
	}

	public void onClick(ClickEvent evt) {
		if(evt.getSource().equals(btnLogin)){
			AsyncCallback<String> callback = new AsyncCallback<String>(){
				public void onSuccess(String result){
					if(result ==null){
						//login successful
						application.updateUI();
						hide();
					}
					else{
						Window.alert(application.getMessages().loginFailure());
						application.updateUI();
						hide();
					}	
				}
				public void onFailure(Throwable caught){
					Window.alert(application.getMessages().loginFailure());
				}
			};
			application.getSessionService().loginEditor(txtName.getText(), txtPassword.getText(), callback);
		}
	}

	public void show(){
		super.show();
		int left = (Window.getClientWidth() - getOffsetWidth()) / 2 + getBodyScrollLeft();
		int top = (Window.getClientHeight() - getOffsetHeight()) / 2 + getBodyScrollTop();
		setPopupPosition(left, top);
	}

	private native int getBodyScrollLeft() /*-{
	    return $doc.body.scrollLeft;
	}-*/;


	private native int getBodyScrollTop() /*-{
	      return $doc.body.scrollTop;
	}-*/;
}	


